package com.qcxk.yuqian.system.controller;

import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.qcxk.yuqian.system.component.behaviour.BehaviourOperator;
import com.qcxk.yuqian.system.component.constant.SystemConstant;
import com.qcxk.yuqian.system.component.exception.ResultBean;

import com.qcxk.yuqian.system.entity.User;
import com.qcxk.yuqian.system.mapper.UserRoleMapper;
import com.qcxk.yuqian.system.service.UserService;
import com.qcxk.yuqian.system.utils.RedisUtil;
import com.qcxk.yuqian.system.utils.ThmsUtil;
import org.apache.commons.codec.digest.DigestUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.lang.reflect.Field;
import java.util.List;

@RestController
@RequestMapping("/user")
public class UserController {

    @Autowired
    private HttpServletRequest request;
    @Autowired
    private UserService userService;
    @Autowired
    private UserRoleMapper userRoleMapper;
    //    @Autowired
//    private UserextraService userextraService;
    @Autowired
    private RedisUtil redisUtil;

    @BehaviourOperator(desc = "查询用户信息")
    @RequestMapping(value = "/loadUser")
    public JSONObject loadUser(User user) {
        JSONObject json = new JSONObject();

            user = userService.getById(user.getId());

        if (user != null) {
            json.put("code", "0");
            json.put("data", user);
        } else {
            json.put("code", "1");
            json.put("msg", SystemConstant.SYS_DATABASE_ERROR_CN);
        }
        return json;
    }

    @BehaviourOperator(desc = "查询员工列表")
    @RequestMapping(value = "/query")
    public JSONObject query(Page page, User user) {
        JSONObject json = new JSONObject();
        page.setDesc("ctime");
//        QueryWrapper<User> userQueryWrapper = new QueryWrapper<>(user);
        page = userService.queryUserForAdminPage(page, user);
        json.put("code", "0");
        json.put("current", page.getCurrent());
        json.put("size", page.getSize());
        json.put("total", page.getTotal());
        json.put("dataList", page.getRecords());
        return json;
    }

    @RequestMapping(value = "/queryUserByUsername")
    public JSONObject queryUserByUsername(String username) {
        JSONObject json = new JSONObject();
        QueryWrapper<User> userQueryWrapper = new QueryWrapper<>();
        userQueryWrapper.like("username", username);
        List<User> userList = userService.list(userQueryWrapper);
        json.put("dataList", userList);
        return json;
    }

    @BehaviourOperator(desc = "修改用户信息")
    @RequestMapping(value = "/updateUser")
    public JSONObject updateUser(User user) {
        JSONObject json = new JSONObject();
        if (userService.updateUser(user)) {
            json.put("code", "0");
        }
        return json;
    }

    @BehaviourOperator(desc = "修改用户部门信息")
    @RequestMapping(value = "/updateUserDept")
    public JSONObject updateUserDept(User user) {
        JSONObject json = new JSONObject();
        if (userService.updateUser(user)) {
            json.put("code", "0");
        }
        return json;
    }

    @BehaviourOperator(desc = "删除用户")
    @RequestMapping(value = "/deleteUserById")
    public JSONObject deleteUserById(User user) {
        JSONObject json = new JSONObject();
        if (userService.deleteUserById(user)) {
            json.put("code", "0");
            //更新缓存
        //    redisUtil.del("user_" + user.getId());
        }
        return json;
    }

    @BehaviourOperator(desc = "单一属性修改SysUsers")
    @RequestMapping(value = "/updateSysUsersSingle")
    public JSONObject updateSysUsersSingle(Integer id, String column, String value) throws NoSuchFieldException, IllegalAccessException {
        JSONObject json = new JSONObject();
        User user = userService.getById(id);
        Class<? extends User> clazz = user.getClass();// 获取到对象对应的class对象
        Field nameField = clazz.getDeclaredField(column);// 获取私有成员变量:column
        nameField.setAccessible(true);// 设置操作权限为true
        nameField.set(user, value);
        if (userService.updateById(user)) {
            json.put("code", "0");
        } else {
            json.put("code", "1");
            json.put("msg", SystemConstant.SYS_DATABASE_ERROR_CN);
        }
        return json;
    }

    @BehaviourOperator(desc = "员工开户")
    @RequestMapping(value = "/addUser")
    public JSONObject addUser(User user) {
        JSONObject json = new JSONObject();
        user.setCtime(ThmsUtil.getDate());
        user.setState("0");
        json.put("code", "0");
        user.setPassword(user.getPassword());
        userService.save(user);
        return json;
    }

    @BehaviourOperator(desc = "物料申请人列表")
    @RequestMapping(value = "/queryUserForMaterialRequisition")
    public JSONObject queryUserForMaterialRequisition() {
        return ResultBean.success(userService.queryUserForMaterialRequisition());
    }

}
